package utils;

import irio.gamer.gamer;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Logic.GamerParams;

public class JDouble extends JPanel{

	private Vec vec = null;
	private Vec from = null;
	private Vec to = null;
	
	public boolean postprocessing = false;
	
	
	JSlider x;
	JFormattedTextField tx;
	private static final float val = 1000;
	private NumberFormat df;

	public JDouble(String name, Vec v, Vec f, Vec t, NumberFormat format, String s) {
		Create(name, v, f,t, format);
		setToolTipText(s);
	}
	
	private void Create(String name, Vec v, Vec f, Vec t, NumberFormat format) {
		vec = v;
		from = f;
		to = t;
		df = format;
		setLayout(new GridLayout(1,3));
//		setBorder(BorderFactory.createLineBorder(Color.gray));
		add(new JLabel(name));
		NumberFormat ff = NumberFormat.getNumberInstance(); 
		ff.setMaximumIntegerDigits(4 );
		tx = new JFormattedTextField(ff);
		add(tx);
		tx.setText(df.format(v.x));
		tx.addActionListener(new ActionListener() {

		
			public void actionPerformed(ActionEvent e) {
				vec.x = Double.parseDouble((tx.getValue().toString()));
				x.setValue((int)(vec.x*val));
			}
		});
		
		x = new JSlider(JSlider.HORIZONTAL,
                (int)(from.x*val), (int)(to.x*val), (int)(vec.x*val));
		add(x);
		x.setPaintTicks(true);
		x.addChangeListener(new ChangeListener() {
			
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				JSlider source = (JSlider)e.getSource();
			    if (!source.getValueIsAdjusting()) {
			        if (df == GamerParams.decimal)
			        	vec.x = (float)(source.getValue()/val);
			        else
			        	vec.x = (int)(source.getValue()/val);
			        	
			        tx.setText(df.format(vec.x));
			        gamer.imagePanel.preview.resetPreview(postprocessing);
			    }
			}
		});


		
	}
	
	public JDouble(String name, Vec v, Vec f, Vec t, NumberFormat format) {
		Create(name, v, f,t, format);

	}
	
	
}
